using System.Text;
using MassTransit;
using MasterNeverDown.EverythingIOT.Model;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using RabbitMQ.Client;

namespace MasterNeverDown.EvetythingIOT.DeviceManagement.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class QueueSenderController(
        IConnection connection)
        : ControllerBase
    {

        [HttpPost("RemoveModbusSubscription")]
        public async Task<IActionResult> SendCommand()
        {
            var requestData = new ModbusSubscription()
            {
                Host = "127.0.0.1",
                Port = 502,
                SlaveId = 1,
                StartAddress = 1,
                NumRegisters = 5
            };
         
            return Ok();
        }
 
       
        [HttpPost("AddModbusSubscription")]
        public async Task<IActionResult> RequestResponse()
        {
            var requestData = new ModbusSubscription()
            {
                Host = "127.0.0.1",
                Port = 502,
                SlaveId = 1,
                StartAddress = 1,
                NumRegisters = 5
            };
            var channel = connection.CreateModel();
            channel.QueueDeclare(queue: "catalogEvents",
                durable: false,
                exclusive: false,
                autoDelete: false,
                arguments: null);
            var body = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(requestData)); 

            channel.BasicPublish(exchange: string.Empty,
                routingKey: "catalogEvents",
                basicProperties: null,
                body: body);


            return Ok();
        }
    }
}